#pragma warning(disable:4005)
#include <drogon/HttpController.h>
#include "OCServer/DocGenerate.h"
#include "MyGrib.h"

using namespace drogon;
using namespace OCPP;

class Weathertoy : public HttpController<Weathertoy>
{
public:
	METHOD_LIST_BEGIN
		METHOD_ADD(Weathertoy::home, "", Get);
	METHOD_ADD(Weathertoy::openGrib, "openGrib", Post);
	METHOD_ADD(Weathertoy::getGribVar, "getGribVar", Get);
	METHOD_ADD(Weathertoy::listGribVar, "listGribVar", Get);
	
	METHOD_LIST_END
protected:
	void home(const HttpRequestPtr& req,
		std::function<void(const HttpResponsePtr&)>&& callback)
	{
		auto resp = HttpResponse::newHttpResponse();
		resp->setBody("Weathertoy");
		callback(resp);
	}

	void openGrib(const HttpRequestPtr& req,
		std::function<void(const HttpResponsePtr&)>&& callback)
	{
		auto resp = HttpResponse::newHttpResponse();
		String data = Singleton(MyGrib).openGrib(req->getBody().data());
		resp->setBody(data);
		callback(resp);
	}

	void getGribVar(const HttpRequestPtr& req,
		std::function<void(const HttpResponsePtr&)>&& callback)
	{
		auto resp = HttpResponse::newHttpResponse();
		String name = req->getParameter("name");
		String timeIndex = req->getParameter("timeIndex");
		String levelIndex = req->getParameter("levelIndex");
		String data = Singleton(MyGrib).getGribVar(name, timeIndex, levelIndex);
		resp->setBody(data);
		callback(resp);
	}

	void listGribVar(const HttpRequestPtr& req,
		std::function<void(const HttpResponsePtr&)>&& callback)
	{
		auto resp = HttpResponse::newHttpResponse();
		String data = Singleton(MyGrib).listGribVar();
		resp->setBody(data);
		callback(resp);
	}
};